package com.iflytek.uoamp.agent.monitor.sender.biz.quick;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class QuickQueue<T> {
	private BlockingQueue<T> queue;
	private int maxSize = 0;

	public int getMaxSize() {
		return maxSize;
	}

	public void setMaxSize(int maxSize) {
		this.maxSize = maxSize;
		queue = new LinkedBlockingQueue<T>(maxSize);
	}

	public QuickQueue(int size) {
		maxSize = size;
		queue = new LinkedBlockingQueue<T>(size);
	}

	public int Size() {
		return queue.size();
	}

	public void Enqueue(T e) throws Exception {
		try {
			queue.add(e);
		} catch (Exception fullException) {
			throw fullException;
		} finally {
			e = null;
		}
	}

	public T Dequeue() {
		T e = queue.poll();
		return e;
	}
	
	public boolean offer(T e, int timeOut)  throws Exception {
		return queue.offer(e, timeOut * 1000, TimeUnit.MILLISECONDS);
	}
	
	public T poll(int timeOut)  throws Exception  {
		T e = queue.poll(timeOut * 1000, TimeUnit.MILLISECONDS);
		return e;
	}
	
}
